from datetime import datetime
from zoneinfo import ZoneInfo

from pydantic import BaseModel, Field, field_serializer


class IdenticalModel(BaseModel):
    id: str = Field(title="ID", min_length=1, serialization_alias="ID")
    last_modified_time: datetime = Field(
        title="最后修改时间",
        json_schema_extra={"uneditable": True, "last_field": True},
        default_factory=lambda: datetime.now(ZoneInfo("Asia/Shanghai")),
        description="最后修改时间，格式为YYYY-MM-DD HH:MM:SS",
        serialization_alias="最后修改时间",
    )
    create_time: datetime = Field(
        title="创建时间",
        json_schema_extra={"uneditable": True, "last_field": True},
        default_factory=lambda: datetime.now(ZoneInfo("Asia/Shanghai")),
        description="创建时间，格式为YYYY-MM-DD HH:MM:SS",
        serialization_alias="创建时间",
    )

    @field_serializer("last_modified_time")
    def serialize_last_modified_time(self, dt: datetime) -> str:
        return dt.astimezone(ZoneInfo("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S")

    @field_serializer("create_time")
    def serialize_create_time(self, dt: datetime) -> str:
        return dt.astimezone(ZoneInfo("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S")


# 使用示例
if __name__ == "__main__":
    model = IdenticalModel(id="1")

    # 序列化为JSON
    print(model.model_dump_json(indent=2))
    print(model.model_dump(include={"id", "create_time"}))
    print(model.create_time)
